<template>
  <div class="container">
    <div class="app-container">
      <el-button
        type="primary"
        style="margin: 10px"
        size="mini"
        @click="handleAddPermission(0, 1)"
        >添加权限</el-button
      >
      <el-card>
        <el-table :data="permissionList" row-key="id" border size="medium">
          <el-table-column
            label="名称"
            prop="name"
            align="center"
            width="160px"
          ></el-table-column>
          <el-table-column label="标识" prop="code"></el-table-column>
          <el-table-column label="描述" prop="description"></el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button
                type="text"
                v-if="row.type == 1"
                @click="handleAddPermission(row.id, 2)"
                >添加</el-button
              >
              <el-button type="text" @click="handleEditPermission(row.id)"
                >编辑</el-button
              >
              <!--  -->
              <el-popconfirm
                :title="`你确定删除${row.name}吗?`"
               
                @onConfirm="deletePermission(row.id)"
                style="margin-left: 10px;"
              >
                <el-button type="text" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 对话框 -->
    <el-dialog
      :title="permissionForm.id ? '修改权限' : '添加权限'"
      :visible.sync="dialogVisible"
      @open="clearForm"
    >
      <el-form
        label-width="120px"
        :model="permissionForm"
        :rules="rules"
        ref="form"
      >
        <el-form-item label="权限名称" prop="name">
          <el-input
            placeholder="请输入权限名称"
            v-model="permissionForm.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input
            placeholder="请输入权限标识"
            v-model="permissionForm.code"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input
            placeholder="请输入权限描述"
            v-model="permissionForm.description"
          ></el-input>
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="permissionForm.enVisible"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div style="text-align: center">
        <el-button type="primary" size="small" @click="handleAddConfirm"
          >确定</el-button
        >
        <el-button type="danger" size="small" @click="dialogVisible = false"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
//引入接口
import {
  getPermissionList,
  addOrUpdatePermission,
  getPermissionInfo,
  deletePermission,
} from "@/api/permission";
//引入工具函数
import { listToTree } from "@/utils";
export default {
  name: "Permission",
  data() {
    return {
      //权限数据
      permissionList: [],
      //控制对话框显示与隐藏
      dialogVisible: false,
      //新增权限的数据
      permissionForm: {
        name: "",
        code: "",
        description: "",
        type: 0,
        pid: 0,
        enVisible: "0",
      },
      //表单校验规则
      rules: {
        name: [
          { required: true, message: "权限名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "权限名称长度在2-10个字符",
            trigger: "blur",
          },
        ],
        code: [
          { required: true, message: "权限标识不能为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "权限标识长度在2-10个字符",
            trigger: "blur",
          },
        ],
        description: [
          {
            required: true,
            message: "权限描述不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  //挂载时调用
  mounted() {
    this.getPermissionList();
  },
  methods: {
    //获取权限列表
    async getPermissionList() {
      let reslut = listToTree(await getPermissionList(), 0);
      this.permissionList = reslut;
    },
    //添加权限的回调
    handleAddPermission(pid, type) {
      
      //存储数据
      this.permissionForm.pid = pid;
      this.permissionForm.type = type;
      //控制对话框显示
      this.dialogVisible = true;
    },
    //确定按钮的回调
    async handleAddConfirm() {
      //校验全部表单
      await this.$refs.form.validate();

      //调用接口
      await addOrUpdatePermission(this.permissionForm);
      //提示
      this.$message({
        type: "success",
        message: this.permissionForm.id ? "权限修改成功" : "权限添加成功",
      });
      //关闭对话框
      this.dialogVisible = false;
      //重新获取数据
      this.getPermissionList();
    },
    //清除表单数据
    clearForm() {
      Object.assign(this.permissionForm, {
        enVisible: 0,
        id:null
      });
      this.$nextTick(() => {
        this.$refs.form.resetFields();
      });
    },
    //编辑按钮的回调
    async handleEditPermission(id) {
      //打开对话框
      this.dialogVisible = true;
      let reslut = await getPermissionInfo(id);
      Object.assign(this.permissionForm, reslut);
    },
    //删除的回调
    async deletePermission(id) {
      console.log(1111);
      //调用接口
      await deletePermission(id);
      //提示
      this.$message({
        type: "success",
        message: "删除成功",
      });
      //重新获取数据
      this.getPermissionList();
    },
  },
};
</script>
